import { request } from '../request';

/**
 * 获取游戏详情
 * @param gameId 游戏ID
 */
export function fetchGameDetail(gameId: number) {
  return request({
    url: '/api/game/detail.do',
    method: 'get',
    params: { gameId }
  });
}

/**
 * 获取当前用户拥有的游戏列表
 *
 * @param userId 用户ID
 */
export function fetchUserGames(userId: string, page?: number, size?: number) {
  return request({
    url: '/api/userGame/list.do',
    method: 'get',
    params: { userId, page, size }
  });
}

/**
 * 获取好友拥有的游戏列表
 *
 * @param friendId 好友用户ID
 */
export function fetchFriendGames(friendId: string) {
  return request({
    url: '/api/userGame/friend.do',
    method: 'get',
    params: { friendId }
  });
}

/** 新增用户游戏（购买/添加游戏） */
export function addUserGame(userGame: { userId: number; gameId: number; playTime?: number }) {
  return request({
    url: '/api/userGame/add.do',
    method: 'post',
    data: userGame
  });
}

/** 更新用户游戏信息 */
export function updateUserGame(userGame: { userId: number; gameId: number; playTime?: number }) {
  return request({
    url: '/api/userGame/update.do',
    method: 'post',
    data: userGame
  });
}

/** 删除用户游戏 */
export function deleteUserGame(userGameId: number) {
  return request({
    url: '/api/userGame/delete.do',
    method: 'post',
    params: { userGameId }
  });
}

/** 检查用户是否拥有某游戏 */
export function checkUserGame(userId: number, gameId: number) {
  return request({
    url: '/api/userGame/check.do',
    method: 'get',
    params: { userId, gameId }
  });
}

/** 获取用户总游玩时长 */
export function fetchUserPlayTime(userId: string) {
  return request<number>({
    url: '/api/userGame/playTime.do',
    method: 'get',
    params: { userId }
  });
}

/** 获取用户最近游玩的游戏 */
export function fetchRecentGames(userId: string, limit: number = 5) {
  return request({
    url: '/api/userGame/recent.do',
    method: 'get',
    params: { userId, limit }
  });
}

// 评论相关接口保留
/** 获取指定游戏的所有评论 */
export function fetchGameReviews(gameId: number) {
  return request<
    Array<{
      id: number;
      gameId: number;
      userId: number;
      content?: string;
      comment?: string;
      rating: number;
    }>
  >({
    url: '/api/review/game.do',
    method: 'get',
    params: { gameId }
  });
}

/** 新增评论 */
export function addGameReview(review: { gameId: number; userId: number; comment: string; rating: number }) {
  return request<null>({
    url: '/api/review/add.do',
    method: 'post',
    data: review
  });
}
